<?php

class App_WebService_ValidateApp {

  public static function validate($app, $methodName) {
    if (empty($app->id) || empty($app->key))
      throw new App_Exception_App(5001);
    
    if (!Core_Core::getInstance()->getApplication()) {
      Core_Core::getInstance()->setApp($app);
      $application = ApplicationsQuery::create()
              ->filterByAppId($app->id)
              ->filterByAppKey($app->key)
              ->findOne();
      if (!$application instanceOf Applications)
        throw new App_Exception_App(5002);

      Core_Core::getInstance()->setApplication($application);
    }
  }

  public static function getUserIpAddr() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
      return $_SERVER['HTTP_CLIENT_IP'];
    } else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
      return $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
      return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
    }
  }

}